<?php
require_once('./class/Financement.php');
require_once('./class/Association.php');
require_once('./class/Partenaire.php');

switch(@$_GET['action']){

    default:
        echo _FINANCEMENT_;
    break;

    case "addForm":
        echo Financement::getAddForm();
        break;

    case "doAdd":
        $f = new Financement();
        $f->setIdPartenaire($_POST['id_partenaire']);
        $f->setIdAsso($_POST['id_association']);
        $f->setMontant($_POST['montant']);
        $f->setDateFinancement($_POST['date_financement']);
        $f->addOrModify();
        $p = Partenaire::getPartenaire($_POST['id_partenaire']);
        echo _FINANCEMENT_." ". _DE_." ".$p->getNom()." (".$f->getMontant().") "._ADDED_;
        break;

    case "modifyForm":
        $f = Financement::getFinancement($_GET['id_financement']);
        echo $f->getModifyForm();
        break;

    case "doModify":
        $f = new Financement();
        $f->setIdFinancement($_POST['id_financement']);
        $f->setIdPartenaire($_POST['id_partenaire']);
        $f->setIdAsso($_POST['id_association']);
        $f->setMontant($_POST['montant']);
        $f->setDateFinancement($_POST['date_financement']);
        $f->addOrModify();
        $p = Partenaire::getPartenaire($_POST['id_partenaire']);
        echo _FINANCEMENT_.' '. _DE_." ".$p->getNom()." "._MODIFIED_;
        break;

    case "list":
        $array = Financement::getAllFinancement();
        $html = '<table class="tablesorter">
						<thead>
							<th>'._ASSOCIATION_.'</th>
							<th>'._PARTENAIRE_.'</th>
							<th>'._MONTANT_FINANCEMENT_.'</th>
							<th>'._DATE_FINANCEMENT_.'</th>
							<th>'._EDIT_.'</th>
							<th>'._DELETE_.'</th>
						</thead><tbody>';
        $i = 0;
        foreach($array as $r){
            $a = Association::getAssociation($r->getIdAsso());
            $p = Partenaire::getPartenaire($r->getIdPartenaire());
            $odd = ($i % 2 == 0) ? 'class="odd"' : "";
            $i++;
            $html .= '<tr '.$odd.'>
						<td>'.$a->getNom().'</td>
						<td>'.$p->getNom().'</td>
						<td>'.$r->getMontant().'</td>
						<td>'.$r->getDateFinancement().'</td>
						<td><a href="?module=financement&action=modifyForm&id_financement='.$r->getIdFinancement().'">'._EDIT_.'</a></td>
						<td><a href="?module=financement&action=delete&id_financement='.$r->getIdFinancement().'">'._DELETE_.'</a></td>
					</tr>';
        }
        $html .= '</tbody></table>';
        echo $html;
        break;

    case "delete":
        $f = Financement::getFinancement($_GET['id_financement']);
        $p = Partenaire::getPartenaire($f->getIdPartenaire());
        $f->delete();
        echo _FINANCEMENT_.' '. _DE_.' '. $p->getNom().' ('.$f->getMontant().') '._DELETED_;
        break;
}
?>
